<?php

namespace app\api\controller;

use app\api\service\HomeService;
use think\facade\Request;
use app\common\Error;
use app\common\Common;

class Home extends Base
{
    private $service;

    public function __construct(HomeService $service)
    {
        $this->service = $service;
    }

    /**
     * 获取首页数据
     * 
     * @dateTime 2022/09/03
     * @author zhixiang0215@126.com
     * @param array params
     * @param int page limit
     * @return json
     */
    public function index()
    {
        $params = Request::param();

        $app_id = $params['app_id'] ?? '';
        $data = [];
        $data['banner'] = $this->service->getHomeBannerList($this->getUserID(),$params);
        $data['recommended'] = $this->service->getRecommendedList($params, $this->getUserID());
        //$data['type_data'] = $this->service->getTypeData();
        $data['type_data'] = $this->service->getCustomVideoTypes($app_id);
        $uid = $this->getUserID();
        $new_params = is_array($params) ? $params : [];
        $data['type_list_1'] = $this->service->getHomeTypeData(array_merge($new_params,['type' => 1]), $uid);
        $data['type_list_2'] = $this->service->getHomeTypeData(array_merge($new_params,['type' => 2]), $uid);
        $data['type_list_3'] = $this->service->getHomeTypeData(array_merge($new_params,['type' => 3]), $uid);
        $data['type_list_4'] = $this->service->getHomeTypeData(array_merge($new_params,['type' => 4]), $uid);

        return Common::success($data);
    }

    /**
     * 获取视频
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param int video_id
     * @param int video_detail_id
     * @return \think\response\Json
     */
    public function getVideoDetail()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Home::class)->scene('get_video')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getVideoDetail($params, $this->getUserID());

        if (!empty($data['message'])) {
            $code = !empty($data['code']) ? $data['code'] : Error::FAIL_CODE;
            return Common::fail($data['message'], intval($code));
        }
        return Common::success($data);
    }

    /**
     * 微信新版播放器详情
     * @return \think\response\Json
     */
    public function wechatVideoDetail()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Home::class)->scene('get_video')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->wechatVideoDetail($params, $this->getUserID());

        if (!empty($data['message'])) {
            $code = !empty($data['code']) ? $data['code'] : Error::FAIL_CODE;
            return Common::fail($data['message'], intval($code));
        }

        return Common::success($data);
    }

    /**
     * 获取短剧数据
     * @return \think\response\Json
     */
    public function getDramaDetail()
    {
        $params = Request::param();
        if (empty($params['video_id'])) {
            return Common::fail('参数错误');
        }

        $data = $this->service->getDramaDetail($params);
        if (!empty($data['message'])) {
            $code = !empty($data['code']) ? $data['code'] : Error::FAIL_CODE;
            return Common::fail($data['message'], intval($code));
        }
        return Common::success($data);
    }

    /**
     * 获取首页类型数据 
     * 
     * @dateTime 2022/09/02
     * @author zhixiang0215@126.com
     * @param int type 类型值  1排行 2最新 3热门
     * @return json
     */
    public function typeList()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Home::class)->scene('type_list')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getHomeTypeData($params, $this->getUserID());

        return Common::success($data);
    }

    /**
     * 获取指定分类的视频数据
     * 
     * @dateTime 2022/09/01
     * @author zhixiang0215@126.com
     * @param int typeId
     * @return array
    */
    public function getListByType()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Home::class)->scene('get_list_by_type')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }
        $params=$this->getResponse($params);
        $data = $this->service->getListByTypeId($params, $this->getUserID());
        return Common::success($data);
    }
      /**
     * 过滤sql
     *
     * @return array
     */
    public function getResponse($params){
        foreach ($params as $key => $value) {
            $filtered_name = addslashes($params[$key]);
            $params[$key]=$filtered_name;
        }
        return $params;
    }
}